<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'

const questions = ref([])
const loading = ref(false)
const router = useRouter()

const fetchQuestions = async () => {
    loading.value = true
    try {
        // Get latest questions with answers
        const response = await fetch('http://127.0.0.1:8000/doctor/question/0/0/')
        const data = await response.json()
        questions.value = data.results?.slice(0, 2) || []
    } catch (error) {
        console.error('Error fetching questions:', error)
    } finally {
        loading.value = false
    }
}


const viewQANewPage = (questionId: number) => {
    const questionUrl = `/qa/${questionId}`;
    window.open(questionUrl, '_blank'); // 在新标签页中打开
}

onMounted(() => {
    fetchQuestions()
})
</script>

<template>
    <div class="qa-container">
        <div class="qa-header">
            <h2 class="qa-title">大家都在问</h2>
            <router-link to="/qa" class="qa-more">查看更多</router-link>
        </div>

        <div class="qa-grid">
            

            <div v-for="question in questions" :key="question.id" class="qa-card"
                @click="viewQANewPage(question.id)">
                    <!-- Question -->
                    <div class="question">
                        <img :src="question.avatar || '/default-avatar.png'" class="avatar" alt="用户头像">
                        <div class="question-content" v-html="question.content.slice(0, 20)"></div>
                    </div>

                    <!-- Answer -->
                    <div v-if="question.answers?.length" class="answer">
                        <div class="doctor-info">
                            <!-- Doctor avatar in a separate column -->
                            <img :src="question.answers[0].doctor.avatar" class="doctor-avatar" alt="医生头像">

                            <!-- Doctor details, answer content, and help count aligned in the same column -->
                            <div class="doctor-details">
                                <div class="doctor-meta">
                                    <span class="doctor-name">{{ question.answers[0].doctor.name }}</span>
                                    <span class="doctor-title">{{ question.answers[0].doctor.get_title_display }}</span>
                                    <span class="doctor-department">{{ question.answers[0].doctor.department }}</span>
                                    <span class="doctor-hospital">{{ question.answers[0].doctor.hospital }}</span>
                                </div>
                                <p class="answer-content" v-html="question.answers[0].content.slice(0, 20)"></p>
                                <span class="help-count">帮助 {{ question.answers[0].like_count }} 人</span>
                            </div>
                        </div>
                    </div>
            
            </div>
        </div>
    </div>
</template>

<style scoped>
.qa-container {
    padding: 20px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    max-width: 1270px;
    margin: 0 auto;
    margin-top: 20px;
}

.qa-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

.qa-title {
    font-size: 18px;
    font-weight: bold;
    color: #333;
}

.qa-more {
    color: #00b38a;
    text-decoration: none;
}

.qa-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;
    
}

.qa-card {
    padding: 15px;
    border: 1px solid #eee;
    border-radius: 8px;
    background: #f9f9f9;
    text-decoration: none;
    color: inherit;
    display: block;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
    /* 添加阴影 */
    transition: transform 0.3s ease, box-shadow 0.3s ease;
    /* 添加交互效果 */
}

.qa-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
}

.question,
.answer {
    margin-bottom: 15px;
}

.avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    margin-right: 10px;
}

.question {
    display: flex;
    align-items: center;
}

.question-content {
    flex: 1;
    margin: 0;
    color: #333;
}

.answer {
    background-color: #cdf8ed8e;
    padding: 10px;
    border-radius: 8px;
}

.doctor-info {
    display: grid;
    grid-template-columns: 40px 1fr;
    gap: 10px;
    align-items: flex-start;
    margin-bottom: 10px;
}

.doctor-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
}

.doctor-details {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
}

.doctor-meta {
    display: flex;
    align-items: flex-end;
    gap: 5px;
    flex-wrap: nowrap;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.doctor-name {
    font-weight: bold;
    color: #333;
}

.doctor-title,
.doctor-hospital,
.doctor-department {
    font-size: 12px;
    color: #666;
}

.answer-content {
    color: #666;
    margin-top: 5px;
}

.help-count {
    color: #00b38a;
    font-size: 12px;
    margin-top: 5px;
}
</style>
